import { _decorator, Button, Component, Label, Node } from 'cc';
import { LayerBase } from '../utils/LayerBase';
import { UserInfo } from '../UserInfo';
import { Main } from './Main';
import { UILayerMgr } from '../utils/UILayerMgr';
import { Adsdk } from './Adsdk';
const { ccclass, property } = _decorator;

@ccclass('TaskPanel')
export class TaskPanel extends LayerBase {
    
    @property(Node)
    taskItemRoot:Node;

    start(){
        Adsdk.instance.showInterstitialAd();
    }

    init(somedData?: any): void {

        //0
        if (UserInfo.taskIsTodayReceiveReward) {
            this.taskItemRoot.children[0].getChildByName("btn").getChildByName("Label").getComponent(Label).string="已领取";
        }
        else{
            this.taskItemRoot.children[0].getChildByName("btn").getChildByName("Label").getComponent(Label).string="领取";
        }

        //2
        if (UserInfo.todayWinPkGameNum >= 1) {
            if (UserInfo.taskIsTodayReceiveWinOnePkReward) {
                this.taskItemRoot.children[2].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "已领取";
            }
            else {
                this.taskItemRoot.children[2].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "领取";

            }
        }
        else {
            this.taskItemRoot.children[2].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "去完成";
        }

        //3
        if (UserInfo.taskTodayWinGateNum >= 1) {
            if (UserInfo.taskIsTodayReceiveGateWinOneReward) {
                this.taskItemRoot.children[3].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "已领取";
            }
            else {
                this.taskItemRoot.children[3].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "领取";

            }
        }
        else {
            this.taskItemRoot.children[3].getChildByName("btn").getChildByName("Label").getComponent(Label).string = "去完成";
        }
    }

    onclickTaskBtn0() {
        let str = this.taskItemRoot.children[0].getChildByName("btn").getChildByName("Label").getComponent(Label).string;

        if (str == "领取") {
            UserInfo.staminaNum += 2;
            UserInfo.taskIsTodayReceiveReward=true;
            Main.ins.createMinTip("获得2点体力");
            Main.ins.saveGameData();
            Main.ins.initHomeUI();
            UILayerMgr.getIns.resfreshUI(this.node.name);
        }

    }
    onclickTaskBtn1() {
        Adsdk.instance.showVideo(()=>{
            UserInfo.staminaNum += 3;
            Main.ins.createMinTip("获得3点体力");
            Main.ins.saveGameData();
            Main.ins.initHomeUI();
            UILayerMgr.getIns.resfreshUI(this.node.name);
        },()=>{

        })
        
    }
    onclickTaskBtn2() {
        let str = this.taskItemRoot.children[2].getChildByName("btn").getChildByName("Label").getComponent(Label).string;
        if (str == "领取") {
            UserInfo.staminaNum += 3;
            Main.ins.createMinTip("获得3点体力");
            UserInfo.taskIsTodayReceiveWinOnePkReward=true;
            Main.ins.saveGameData();
            Main.ins.initHomeUI();
            UILayerMgr.getIns.resfreshUI(this.node.name);
        }
        else if (str == "去完成") {
            UILayerMgr.getIns.closeUILayer(this.node.name);
            Main.ins.onclickstartMathGameBtn();
        }

    }
    onclickTaskBtn3() {
        let str = this.taskItemRoot.children[3].getChildByName("btn").getChildByName("Label").getComponent(Label).string;
        if (str == "领取") {
            UserInfo.staminaNum += 3;
            Main.ins.createMinTip("获得3点体力");
            UserInfo.taskIsTodayReceiveGateWinOneReward=true;
            Main.ins.saveGameData();
            Main.ins.initHomeUI();
            UILayerMgr.getIns.resfreshUI(this.node.name);
        }
        else if (str == "去完成") {
            UILayerMgr.getIns.closeUILayer(this.node.name);
            Main.ins.onclickstartGateGameBtn();
        }

    }
}


